/**
 * 游戏错误基类
 * 定义统一的错误处理接口
 */
export abstract class GameError extends Error {
    abstract readonly code: string;
    abstract readonly statusCode: number;
    public readonly timestamp: Date;
    public readonly context?: any;

    constructor(message: string, context?: any) {
        super(message);
        this.name = this.constructor.name;
        this.timestamp = new Date();
        this.context = context;
        
        // 确保堆栈跟踪正确指向原始错误位置
        if (Error.captureStackTrace) {
            Error.captureStackTrace(this, this.constructor);
        }
    }

    /**
     * 转换为客户端安全的错误信息
     */
    toClientSafe(): ClientSafeError {
        return {
            code: this.code,
            message: this.message,
            timestamp: this.timestamp
        };
    }

    /**
     * 转换为详细的错误信息（用于日志）
     */
    toDetailedInfo(): DetailedErrorInfo {
        return {
            code: this.code,
            message: this.message,
            statusCode: this.statusCode,
            timestamp: this.timestamp,
            context: this.context,
            stack: this.stack
        };
    }

    /**
     * 检查是否为特定类型的错误
     */
    static isGameError(error: any): error is GameError {
        return error instanceof GameError;
    }
}

/**
 * 客户端安全错误信息
 */
export interface ClientSafeError {
    code: string;
    message: string;
    timestamp: Date;
}

/**
 * 详细错误信息
 */
export interface DetailedErrorInfo {
    code: string;
    message: string;
    statusCode: number;
    timestamp: Date;
    context?: any;
    stack?: string;
}
